﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.gameState;
using Imaginecup.Recycling.GameMan;
using Imaginecup.recycling.Object;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.util.vertex2D;
using Imaginecup.recycling.GameMan;
using Imaginecup.Engine;
using Imaginecup.util.input;
using Imaginecup.util;
using Microsoft.Xna.Framework.Input;
using Imaginecup.recycling.Stage;
using Imaginecup.gameState.recycling.GameMan;
using Imaginecup.factory;
using Imaginecup.gameState.menu;


namespace Imaginecup.Recycling
{
    public class RecycleGameState : GameState
    {
        private ContentManager m_Content;
        private SpriteBatch spriteBatch;
        private IManager m_StageManager;

        public RecycleGameState()
        {
//            //블레이드 관련
//             Vertex2DPipeMan pipeMan = Vertex2DPipeMan.GetRenderPipeMan();
// 
//             pipeMan.SetScreenSize(new Vector2(800, 480));
//             pipeMan.InitPipeMan2D();
        }

        protected override void InitState()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content/Recycle/";
        }

        protected override void LoadStateResource()
        {
#if WINDOWS_PHONE
            GestureInputHandler.InitInput(m_Game);
            m_Game.Components.Add(GestureInputHandler.GetInputHandler());
#endif
#if WINDOWS
            m_Game.Components.Add(KeyboardInputHandler.GetInputHandler());
#endif
#if XBOX
            m_Game.Components.Add(GamePadHandler.GetInputHandler());
#endif

            //사용할 리소스 초기화
            //Camera.WorldRectangle = new Rectangle(0, 0, StageInfo1.MapWidth, StageInfo1.MapHeight);
            Camera.ViewportWidth = 800;
            Camera.ViewportHeight = 480;

            ResourceManager manager = ResourceManager.GetInstance();
            manager.SetTexture("SpriteSheet", m_Content.Load<Texture2D>("SpriteSheet"));
            manager.SetTexture("Items", m_Content.Load<Texture2D>("Items"));
            
            manager.SetTexture("background", m_Content.Load<Texture2D>("background"));
            manager.SetTexture("back_layer", m_Content.Load<Texture2D>("back_layer"));
            manager.SetTexture("Background_front", m_Content.Load<Texture2D>("front_layer"));
            manager.SetTexture("Hero_Run", m_Content.Load<Texture2D>("Hero_Run"));
            manager.SetTexture("Hero_Jump", m_Content.Load<Texture2D>("Hero_Jump"));
            manager.SetTexture("Hero_Sliding", m_Content.Load<Texture2D>("Hero_Sliding"));
            
            //아이템 인벤토리 관련
            manager.SetTexture("CenterBar", m_Content.Load<Texture2D>("UI/run_centerbar"));
            manager.SetTexture("GLASS", m_Content.Load<Texture2D>("UI/run_but_bottle"));
            manager.SetTexture("CAN", m_Content.Load<Texture2D>("UI/run_but_can"));
            manager.SetTexture("PLASTIC", m_Content.Load<Texture2D>("UI/run_but_plastic"));
            manager.SetTexture("PAPER", m_Content.Load<Texture2D>("UI/run_but_paper"));
            manager.SetTexture("ETC", m_Content.Load<Texture2D>("UI/run_but_etc"));
            manager.SetTexture("SlidingButton", m_Content.Load<Texture2D>("UI/run_but_slide"));
            manager.SetTexture("JumpButton", m_Content.Load<Texture2D>("UI/run_but_jump"));
            manager.SetTexture("PauseButton", m_Content.Load<Texture2D>("../ui/icon_stop"));
            manager.SetTexture("ClickButton", m_Content.Load<Texture2D>("UI/run_but_click"));
            manager.SetTexture("InvisibleButton", m_Content.Load<Texture2D>("UI/run_but_inact"));
            //펫 관련
            manager.SetTexture("PET_RUN", m_Content.Load<Texture2D>("run_monster_moving"));
            manager.SetTexture("PET_EAT", m_Content.Load<Texture2D>("run_monster_eat"));
            manager.SetTexture("PET_DDONG", m_Content.Load<Texture2D>("run_monster_ddong"));
            //펫 모션 관련
            manager.SetTexture("PET_GLASS", m_Content.Load<Texture2D>("run_icon_recy_bottle"));
            manager.SetTexture("PET_CAN", m_Content.Load<Texture2D>("run_icon_recy_can"));
            manager.SetTexture("PET_ETC", m_Content.Load<Texture2D>("run_icon_recy_etc"));
            manager.SetTexture("PET_PAPER", m_Content.Load<Texture2D>("run_icon_recy_paper"));
            manager.SetTexture("PET_PLASTIC", m_Content.Load<Texture2D>("run_icon_recy_plastic"));

//            // 블레이드 이펙트 리소스
//             manager.SetTexture("streak1", m_Content.Load<Texture2D>("../blade\\streak1"));
//             manager.SetTexture("streak2", m_Content.Load<Texture2D>("../blade\\streak2"));
//             manager.SetTexture("streak3", m_Content.Load<Texture2D>("../blade\\streak3"));
//             manager.SetTexture("streak4", m_Content.Load<Texture2D>("../blade\\streak4"));

            // 스테이지 매니저 생성하고 기본 초기화
            m_StageManager = new StageManager();
            m_StageManager.Initialize();
        }

        protected override void UnloadResource()
        {
            ResourceManager.GetInstance().Release();
            m_Content.Unload();
#if WINDOWS_PHONE
            m_Game.Components.Remove(GestureInputHandler.GetInputHandler());
#endif
#if WINDOWS
            m_Game.Components.Remove(KeyboardInputHandler.GetInputHandler());
#endif
#if XBOX
            m_Game.Components.Remove(KeyboardInputHandler.GetInputHandler());
#endif
        }
        public override void AddStateEvent()
        {
            base.AddStateEvent();
            if(m_GameStateMan.GetTopState() != this)
                Enabled = false;
            else
                Enabled = true;
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch = m_GraphicData.GetSpriteBatch();

            m_StageManager.GetStage().Draw(spriteBatch, gameTime);
        }

        public override void Update(GameTime gameTime)
        {
#if XBOX
            IGamePadHandler gamePad = GamePadHandler.GetInputHandler();
            if(gamePad.IsOneDown(Buttons.Start))
            {
                m_GameStateMan.PushState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Menu));
            }
#endif
#if WINDOWS_PHONE
            IPhoneInputHandler input = PhoneInputHandler.GetInputHandler();
            TouchCollection collection = input.GetCurrentTouch();
            if (input.GetCurrentTouch().Count >= 2)
            {
                TouchTrailLayer.GetTouchTrailLayer().TouchEnd(collection[0]);
                m_GameStateMan.PushState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Menu));
            }
#endif
            m_StageManager.GetStage().Update(gameTime);
            base.Update(gameTime);
        }

    }
}
